package com.woniu.user.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.result.Result;
import com.woniu.user.client.AddressQueryClient;
import com.woniu.user.client.result.AddressResultInfo;
import com.woniu.user.client.result.CityResultInfo;
import com.woniu.user.infra.model.Address;
import com.woniu.user.service.AddressService;
import com.woniu.user.service.CityService;
import com.woniu.user.service.dto.AddressDto;
import com.woniu.user.service.dto.CityDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class AddressQueryApiImpl implements AddressQueryClient {

    @Resource
    private CityService cityService;

    @Resource
    private AddressService addressService;

    //查询所有地址类型 *不是用户个人
    @Override
    public Result<List<CityResultInfo>> getAddType() throws JsonProcessingException {
        List<CityDto> type = cityService.findType();
        List<CityResultInfo> cityResultInfos = BeanUtil.copyToList(type, CityResultInfo.class);
        return Result.getInstanceList(CityResultInfo.class).setMsg("查询成功").setData(cityResultInfos);
    }

    //查询用户地址
    @Override
    public Result<List<AddressResultInfo>> getUserAddress(Integer userId, Integer addressId){
        log.info("查看地址参数:{}{}", userId, addressId);

        List<AddressDto> addressDtos = addressService.listUserAddress(userId,addressId);
        List<AddressResultInfo> addressResultInfos = BeanUtil.copyToList(addressDtos, AddressResultInfo.class);
        return Result.getInstanceList(AddressResultInfo.class).setMsg("查询成功").setData(addressResultInfos);
    }

    @Override
    public Result<AddressResultInfo> getUserAddressById(Integer addressId) {
        log.info("根据ID查询地址:{}", addressId);
        AddressDto addressDto=addressService.getUserAddressById(addressId);
        AddressResultInfo addressResultInfo = BeanUtil.copyProperties(addressDto, AddressResultInfo.class);
        return Result.getInstance(AddressResultInfo.class).setMsg("查询成功").setCode(200).setData(addressResultInfo);
    }
}
